<template>
  <el-dialog v-model="dialogData.visible" :title="dialogData.title"
             center @open="openDialogEvent" class="food-dialog">
    <el-form ref="formRef" :model="dialogData.data"
             :rules=rules label-width="80px">
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="美食名称" prop="name">
            <el-input v-model="dialogData.data.name" autocomplete="off"
                      placeholder="请输入美食名称" clearable
                      show-word-limit maxlength="20">
            </el-input>
          </el-form-item>

          <el-form-item label="美食菜系" prop="category">
            <el-select v-model="dialogData.data.category">
              <el-option v-for="(item, index) in foodCategoryList"
                         :key="index" :label="item" :value="item"/>
            </el-select>
          </el-form-item>

          <el-form-item label="推荐指数" prop="recommendRate">
            <el-rate v-model="dialogData.data.recommendRate"
                     :colors="foodStore.rateColorList"/>
          </el-form-item>

          <el-form-item label="食材">
            <food-material-form />
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="步骤">
            <food-step-form />
          </el-form-item>

          <el-form-item label="备注">
            <el-input v-model="dialogData.data.remark" autocomplete="off"
                      placeholder="请输入备注" clearable
                      show-word-limit maxlength="20">
            </el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <template #footer>
      <div class="dialog-footer">
        <el-button @click="cancelClick">取消</el-button>
        <el-button type="primary" @click="confirmClick">保存</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import FoodMaterialForm from '@/components/Food/Form/FoodMaterialForm.vue'
import FoodStepForm from '@/components/Food/Form/FoodStepForm.vue'
import { useFoodStore } from '@/store/food.ts'
import { FormInstance, FormRules } from 'element-plus'
import { computed, nextTick, ref, reactive } from 'vue'
import { foodRules } from '@/utils/element/elRules.ts'

const rules = reactive<FormRules>(foodRules)
const formRef = ref<FormInstance>()
const foodStore = useFoodStore()

const foodCategoryList = ['家常菜', '淮扬菜', '鲁菜', '川菜', '粤菜', '闽菜', '浙菜', '湘菜', '徽菜', '其他']

const dialogData = computed(() => foodStore.foodRecipeDialog)

/**
 * 打开对话框事件
 */
const openDialogEvent = async () => {
  await nextTick(() => {
    formRef.value?.clearValidate()
  })
}


/**
 * 点击确认按钮
 */
const confirmClick = () => {
  formRef.value?.validate().then(() => {
    foodStore.handleFoodRecipeApi()
  })
}

const cancelClick = () => {
  dialogData.value.visible = false
}
</script>

<style lang="scss">
.food-dialog {
  width: 75%;

  .el-dialog__body {
    height: 65vh;

    .el-button.is-text {
      padding: 0;
    }

    .material-table {
      height: 45vh;
    }

    .food-step-table {
      height: 55vh;
    }
  }
}
</style>
